package com.zhongying.jf.result;


import com.zhongying.jf.ResConstants;
import com.zhongying.jf.rule.Rule;
import org.apache.commons.lang.StringUtils;

import java.io.Serializable;

/**
 * @description result简单封装
 * @auther zengli
 * @create 2017/9/26
 */
public class ResultSimple implements Serializable {

	private static final long serialVersionUID = -5955077279174665691L;

	private String code;

	private String msg;

	private Object model;

	private ResultSimple() {
	}

	public static ResultSimple success() {
		return success(null,null);
	}


	public static ResultSimple success(Object model) {
		return success(null,null,model);
	}

	public static ResultSimple success(String code, String message) {
		return success(code, message, null);
	}

	public static ResultSimple success(String code,String message, Object model) {
		if(StringUtils.isBlank(code)){
			code = ResConstants.SUCCESS.getCode();
		}
		if(StringUtils.isBlank(message)){
			message = ResConstants.SUCCESS.getMsg();
		}
		ResultSimple result = new ResultSimple();
		result.code = code;
		result.msg = message;
		result.model = model;
		return result;
	}

	public static ResultSimple fail(String code, String message, Object model) {
		if(StringUtils.isBlank(code)){
			code = ResConstants.FAIL.getCode();
		}
		if(StringUtils.isBlank(message)){
			message = ResConstants.FAIL.getMsg();
		}

		ResultSimple result = new ResultSimple();
		result.code = code;
		result.msg = message;
		result.model = model;
		return result;
	}

	public static ResultSimple fail(String code, String message) {
		return fail(code, message, null);
	}

	public static ResultSimple fail(String message) {
		return fail(null, message, null);
	}

	public static ResultSimple fail(Rule rule) {
		return fail(rule.code(),rule.content());
	}

	public static ResultSimple fail(Rule rule,Object model) {
		return fail(rule.code(),rule.content(),model);
	}

	public String getCode() {
		return code;
	}

	public String getMsg() {
		return msg;
	}

	public Object getModel() {
		return model;
	}
}
